﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;
using JSIF_WCFServer.Misc.Logging;

namespace JSIF_WCFServer.Server
{
    /// <summary>
    /// A service that serves static files
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, AddressFilterMode = AddressFilterMode.Any)]
    public class StaticFiles : IStaticFiles
    {
        private GenericLogger log = GenericLogger.GetLogger<StaticFiles>();

        /// <summary>
        /// Fetches the contents of the file with the specified name
        /// </summary>
        /// <param name="name">the name of the file</param>
        /// <returns>the contents of the file with the specified name</returns>
        public Stream Fetch(string name)
        {
            log.Debug("static file requested: {0}", name);
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";

            MemoryStream ms = new MemoryStream();
            byte[] temp = Encoding.UTF8.GetBytes("static file2:" + name);
            ms.Write(temp, 0, temp.Length);
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }
    }
}
